package com.zdkj.subway.check.controller.stationTiecketManager.xianWngCJXXQR;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.subway.check.controller.CommonController;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwStApplyNetworkCheckPassengerAffairs;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwStApplyNetworkCheckPassengerAffairsMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.util.UserInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表差错信息
 */
@RestController
@RequestMapping("/xw")
@Slf4j
public class Xw_SwStApplyNetworkCheckPassengerAffairsCtrl {

    @Autowired
    private CommonController commonController;
    @Autowired
    SwStApplyNetworkCheckPassengerAffairsMapper swStApplyNetworkCheckPassengerAffairsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 申请线网协查乘客事务
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/get_xw_SwStApplyNetworkCheckPassengerAffairs")
    @ApiOperation(value = "申请线网协查乘客事务", notes = "申请线网协查乘客事务")
    public Page get_xw_SwStApplyNetworkCheckPassengerAffairs(Page page,@RequestParam Map<String,String> param){

		PigxUser user = SecurityUtils.getUser();
        if(!user.getBelongToLine()){
			String lineCode = user.getLineCode();
			String stationCode = user.getStationCode();
			param.put("lineCode",lineCode);
			param.put("stationCode",stationCode);//只查本车站
		}
		
        List<Map<String, Object>> swStApplyNetworkCheckPassengerAffairs = swStApplyNetworkCheckPassengerAffairsMapper.get_xw_SwStApplyNetworkCheckPassengerAffairs(param);
        page.setData(swStApplyNetworkCheckPassengerAffairs,true);
        return page;
    }

    /**
     * 提交申请线网协查乘客事务
     * @param
     * @param param
     * @return
     */
    @RequestMapping("/submit_xw_SwStApplyNetworkCheckPassengerAffairs")
    @ApiOperation(value = "提交申请线网协查乘客事务", notes = "提交申请线网协查乘客事务")
    @Transactional(rollbackFor = Exception.class)
    public Result submit_xw_SwStApplyNetworkCheckPassengerAffairs(@RequestBody Map<String,Object> param){
        String ids = String.valueOf(param.get("ids"));
        Result res=new Result(ResultDetailMsg.OK);
        try {
            SwStApplyNetworkCheckPassengerAffairs swStApplyNetworkCheckPassengerAffairs=new SwStApplyNetworkCheckPassengerAffairs();
            swStApplyNetworkCheckPassengerAffairs.setApplyStatus("1");
            swStApplyNetworkCheckPassengerAffairs.setLineStatus("0");
            swStApplyNetworkCheckPassengerAffairs.setUpdateTime(new Date());
            UpdateWrapper<SwStApplyNetworkCheckPassengerAffairs> updateWrapper=new UpdateWrapper<>();
            updateWrapper.in("id",ids.split(","));
            swStApplyNetworkCheckPassengerAffairsMapper.update(swStApplyNetworkCheckPassengerAffairs,updateWrapper);
            commonController.innerBatch(param);
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return res;
    }

    /**
     * 删除（该状态）
     * @param ids
     * @return
     */
    @RequestMapping("/delete_xw_SwStApplyNetworkCheckPassengerAffairs")
    @ApiOperation(value = "删除（该状态）", notes = "删除（该状态）")
    public Result delete_xw_SwStApplyNetworkCheckPassengerAffairs(String ids){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            SwStApplyNetworkCheckPassengerAffairs swStApplyNetworkCheckPassengerAffairs=new SwStApplyNetworkCheckPassengerAffairs();
            swStApplyNetworkCheckPassengerAffairs.setDelFlag("1");
            swStApplyNetworkCheckPassengerAffairs.setUpdateTime(new Date());
            UpdateWrapper<SwStApplyNetworkCheckPassengerAffairs> updateWrapper=new UpdateWrapper<>();
            updateWrapper.in("id",ids.split(","));
            swStApplyNetworkCheckPassengerAffairsMapper.update(swStApplyNetworkCheckPassengerAffairs,updateWrapper);
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }

    /**
     * 编辑申请线网协查乘客事务
     * @param param
     * @return
     */
    @RequestMapping("/update_xw_SwStApplyNetworkCheckPassengerAffairs")
    @ApiOperation(value = "编辑申请线网协查乘客事务", notes = "编辑申请线网协查乘客事务")
    public Result update_xw_SwStApplyNetworkCheckPassengerAffairs(@RequestBody JSONObject param){
        Result res=new Result(ResultDetailMsg.OK);

        try {
            SwStApplyNetworkCheckPassengerAffairs swStApplyNetworkCheckPassengerAffairs = JSONObject.parseObject(param.toJSONString(), SwStApplyNetworkCheckPassengerAffairs.class);

            //存在更新 不存在新增
            if(swStApplyNetworkCheckPassengerAffairs.getId()==null){
                swStApplyNetworkCheckPassengerAffairs.setCreateTime(new Date());
                swStApplyNetworkCheckPassengerAffairs.setCreatorId(UserInfo.getId());
                swStApplyNetworkCheckPassengerAffairs.setCreatorName(UserInfo.getUserName());
                swStApplyNetworkCheckPassengerAffairs.setDelFlag("0");
                swStApplyNetworkCheckPassengerAffairs.setFile(StringUtils.isEmpty(swStApplyNetworkCheckPassengerAffairs.getFile())?"":swStApplyNetworkCheckPassengerAffairs.getFile());
                swStApplyNetworkCheckPassengerAffairs.setApplyStatus("0");
                swStApplyNetworkCheckPassengerAffairs.setUpdateTime(new Date());
                swStApplyNetworkCheckPassengerAffairsMapper.insert(swStApplyNetworkCheckPassengerAffairs);
            }else if(swStApplyNetworkCheckPassengerAffairs.getId()!=null && swStApplyNetworkCheckPassengerAffairs.getId()>0){
                swStApplyNetworkCheckPassengerAffairsMapper.updateById(swStApplyNetworkCheckPassengerAffairs);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }

        return res;
    }

    /**
     * 获取线路人员
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSysUserToLine")
    @ApiOperation(value = "获取线路人员", notes = "获取线路人员")
    public Page getSysUserToLine(Page page,@RequestParam Map<String,String> param){
        Map<String,Object> p=new HashMap<>();
        p.put("belong_to_line","1");
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p);
        page.setData(sysUsers);
        return page;
    }

    /**
     * 获取线路人员
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getSysUserToLineSelect")
    @ApiOperation(value = "获取线路人员", notes = "获取线路人员")
    public List<SysUser> getSysUserToLineSelect(Page page,@RequestParam Map<String,String> param){
        Map<String,Object> p=new HashMap<>();
        p.put("belong_to_line","1");
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p);
        return sysUsers;
    }

}
